//
//  ZYTableViewCell.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/8.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "ZYTableViewCell.h"
#import "OYCountDownManager.h"
@implementation ZYTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.timeLable = [[UILabel alloc]init];
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:kCountDownNotification object:nil];
        
    }
    return self;
}

#pragma mark - 倒计时通知回调
- (void)countDownNotification {
    /// 判断是否需要倒计时 -- 可能有的cell不需要倒计时,根据真实需求来进行判断
    if (0) {
        return;
    }
    
    /// 计算倒计时
    NSInteger countDown = [self.countStr integerValue] - kCountDownManager.timeInterval;
    
    if (countDown < 0)
    {
        self.timeLable.text = @"订单已关闭";
        return;
    }
        
    
    /// 重新赋值

    self.timeLable.text = [NSString stringWithFormat:@"等待付款，订单将在%02ld分%02ld秒关闭", (countDown/60)%60, countDown%60];
    
    /// 当倒计时到了进行回调
    if (countDown <= 0) {
        self.timeLable.text = @"订单已关闭";
        if (self.countDownZero) {
            self.countDownZero();
        }
    }
}

///  重写setter方法
-(void)setTimeLable:(UILabel *)timeLable
{
    _timeLable = timeLable;
    [self countDownNotification];
}
- (void)setCountStr:(NSString *)countStr
{
    _countStr = countStr;
    [self countDownNotification];
}
- (void)setTitleStr:(NSString *)titleStr
{
    _titleStr = titleStr;
    [self countDownNotification];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
